package cn.com.controler.Vipapplication;
import cn.com.controler.Util.JDBCUtil;
import cn.com.entity.Vipapplication;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class VipapplicationDAOTemplete {
    //添加数据
    public static int insertdata(String str,Object[]arr)
    {
        Connection coll = null;
        PreparedStatement statement = null;
        try {
            coll = JDBCUtil.getConnection();//1.2步
            statement = coll.prepareStatement(str);//3步，获得的语句对象
            //设置参数
            for (int i = 0; i < arr.length ; i++) {
                statement.setObject(i+1,arr[i]);
            }
            //执行
            return statement.executeUpdate(); //4步执行SQL语句
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(coll,statement,null);// 5步关闭资源
        }
        return 0;
    }


    //查询数据
    public static List selectdata(String str, Object[]arr)
    {
        Connection coll = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List list = new ArrayList();
        try {
            coll = JDBCUtil.getConnection();
            statement = coll.prepareStatement(str);
            //设置参数
            for (int i = 0; i < arr.length ; i++) {
                statement.setObject(i+1,arr[i]);
            }
            //执行
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                Date vipzctime = resultSet.getTimestamp("VipUserRegisterTime");
                Date vipkstime = resultSet.getTimestamp("VipUserAppStartTime");
                Date vipjstime = resultSet.getTimestamp("VipUserAppEndTime");
                SimpleDateFormat sa = new SimpleDateFormat("yyyy-MM-dd");
                String zctime = sa.format(vipzctime);
                String kstime = sa.format(vipkstime);
                String jstime = sa.format(vipjstime);
                Vipapplication vipapplication = new Vipapplication();
                vipapplication.setVipId(resultSet.getInt(1));
                vipapplication.setAppVipId(resultSet.getInt(2));
                vipapplication.setUsername(resultSet.getString(3));
                vipapplication.setUserpassword(resultSet.getString(4));
                vipapplication.setUserEmail(resultSet.getString(5));
                vipapplication.setVipUserRegisterTime(zctime);
                vipapplication.setVipUserAppStartTime(kstime);
                vipapplication.setVipUserAppEndTime(jstime);
                vipapplication.setVipUserAppWhy(resultSet.getString(9));
                vipapplication.setVipUserAppState(resultSet.getInt(10));
                list.add(vipapplication);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(coll,statement,resultSet);
        }
        return null;
    }

}
